//
//  SDHSportsMineVC.m
//  SDHSportsNews
//
//  Created by MichaelShan on 2021/3/3.
//

#import "SDHSportsMineVC.h"
#import "SDHSportsMineHeaderView.h"
#import "SDHSportsMineTableCell.h"
#import "SDHSportsFollowVC.h"
#import "SDHSportsRegisterAndLoginVC.h"
#import "SDHSportsPlayerInfoVC.h"
#import "SDHSportsUserModel.h"
#import "SDHSportsDataManager.h"
#import "SDHSportsPersonalProfile.h"
#import "SDHSportsFeedbackVC.h"
#import "SDHSportsAboutVC.h"

static const CGFloat kHeaderHeight = 285;


@interface SDHSportsMineVC () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *mineTable;
@property (nonatomic, strong) SDHSportsMineHeaderView *headerView;
@property (nonatomic, strong) SDHSportsUserModel *user;

@end

@implementation SDHSportsMineVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    
    // 登录成功
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSucceed:) name:@"login_succeed" object:nil];
    
    [self initUI];
    [self loadData];
    [self updateLoginView];
}

- (void)loadData {
    _user = [SDHSportsDataManager sharedManager].user;
}

- (void)updateLoginView {
    if (_user.phone.length > 0) {
        [_headerView login];
        _headerView.lbSign.text = _user.phone;
    } else {
        [_headerView logout];
    }
}

- (void)loginSucceed:(NSNotification *)notifier {
    _user = [notifier object];
    if (_user.name.length == 0) {
        if (_user.phone.length > 4) {
            _user.name = [_user.phone substringAtRange:NSMakeRange(_user.phone.length-4, 4)];
        } else {
            _user.name = _user.phone;
        }
        
        [SDHSportsDataManager sharedManager].user = _user;
    }
    
    [self updateLoginView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)login {
    if (_user.phone.length == 0) {
        SDHSportsRegisterAndLoginVC *vc = [[SDHSportsRegisterAndLoginVC alloc] initWithNibName:@"SDHSportsRegisterAndLoginVC" bundle:nil];
        vc.hidesBottomBarWhenPushed = YES;
        vc.viewType = ViewTypeLogin;
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        [self enterProfile];
    }
}

- (void)infoClick:(NSInteger)index {
    NSLog(@"personal info click: %d", index);
    
    if (_user.phone.length == 0) {
        SDHSportsRegisterAndLoginVC *vc = [[SDHSportsRegisterAndLoginVC alloc] initWithNibName:@"SDHSportsRegisterAndLoginVC" bundle:nil];
        vc.hidesBottomBarWhenPushed = YES;
        vc.viewType = ViewTypeLogin;
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }
    
    if (index == 1001 || index == 1002) {
        FollowViewType type = TypeViewFollow;
        if (index == 1002) {
            type = TypeViewBrowse;
        }
        
        SDHSportsFollowVC *vc = [[SDHSportsFollowVC alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        vc.viewType = type;
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        [self enterProfile];
    }
}

- (void)enterProfile {
    SDHSportsPersonalProfile *vc = [[SDHSportsPersonalProfile alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)initUI {
    UITableView *table = [UITableView new];
    table.delegate = self;
    table.dataSource = self;
    table.backgroundColor = UIColorFromRGB(250, 250, 250);
    [self.view addSubview:table];
    self.mineTable = table;
    
    UIView *footer = [UIView new];
    footer.backgroundColor = [UIColor whiteColor];
    self.mineTable.tableFooterView = footer;
    
    self.headerView = [SDHSportsMineHeaderView new];
    __weak typeof(self) weakSelf = self;
    self.headerView.loginClick = ^{
        [weakSelf login];
    };
    self.headerView.personInfoClick = ^(NSInteger index) {
        [weakSelf infoClick:index];
    };
    self.mineTable.tableHeaderView = self.headerView;
    
    [self.mineTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.view).insets(UIEdgeInsetsZero).priorityLow();
        make.top.equalTo(weakSelf.view).offset(20);
    }];
}

- (void)viewWillLayoutSubviews {
    self.mineTable.tableHeaderView.frame = CGRectMake(0, 0, kScreenWidth, kHeaderHeight);
}

#pragma mark - setting
- (void)clearCache {
    UIAlertAction *clear = [UIAlertAction actionWithTitle:@"Clear" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [[SDHSportsDataManager sharedManager] clearCache];
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Are you sure Clear Cache?"
                                                                     message:@""
                                                              preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:clear];
    [alert addAction:cancel];
    
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)customerFeedback {
    SDHSportsFeedbackVC *vc = [SDHSportsFeedbackVC new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)aboutUs {
    SDHSportsAboutVC *vc = [SDHSportsAboutVC new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)logout {
    if (_user.phone.length == 0) {
        return;
    }
    UIAlertAction *clear = [UIAlertAction actionWithTitle:@"Logout" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [[SDHSportsDataManager sharedManager] logout];
        _user = nil;
        [self updateLoginView];
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Are you sure Clear Cache?"
                                                                     message:@""
                                                              preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:clear];
    [alert addAction:cancel];
    
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark - tableview
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SDHSportsMineTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[SDHSportsMineTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    cell.nameLb.textColor = [UIColor blackColor];
    if (0 == indexPath.row) {
        cell.nameLb.text = @"Clear cache";
    }
    if (1 == indexPath.row) {
        cell.nameLb.text = @"Customer feedback";
    }
    if (2 == indexPath.row) {
        cell.nameLb.text = @"About us";
    }
    if (3 == indexPath.row) {
        cell.nameLb.text = @"Logout";
        cell.nameLb.textColor = [UIColor redColor];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (0 == indexPath.row) {
        [self clearCache];
    } else if (1 == indexPath.row) {
        [self customerFeedback];
    } else if (2 == indexPath.row) {
        [self aboutUs];
    } else if (3 == indexPath.row) {
        [self logout];
    }
//    SDHSportsPlayerInfoVC *vc = [SDHSportsPlayerInfoVC new];
//    vc.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:vc animated:YES];
}


@end
